<?php
$_log['bankerWiner'] = 0;

$red['db'] = 5; # 底包
$red['coin'] = 100; # 红包的金额
# 模似玩家
$uids = '1221,1163,1155,1073,831';
$sql = "SELECT uid,uno,nickname,recharge,betcoin,profit,(20) AS chance FROM `@@users` WHERE `uid` IN(".$uids.")";
$users = $this->db->query($sql, 3);

# 取10倍牛的所有牌型赔率
$sql = "SELECT * FROM `@@games_odds` WHERE `gameid`=3";
$game['odds'] = $this->db->query($sql, 3);

$gameodds = $game['odds'];
// shuffle($gameodds);

/*
# 随机 5 条数据，产生 5 个红包
$keys = array_rand($gameodds, 5);

$redodds = array();
$decimal = array();
$decimalsum = 0;
foreach($keys as $k=>$v) {
    $redodds[$k] = $gameodds[$v];
    $cards = explode(',', $redodds[$k]['cards']);
    # 随机 1 个尾数出来
    $decimalKey = array_rand($cards, 1);
    $decimal[$k]['coin'] = '0.' . $cards[$decimalKey];
    $decimalsum += floatval($decimal[$k]['coin']);
}

$endcoin = $this->formatCoin(($red['db'] - $decimalsum));
$endcoin = explode('.', $endcoin);

$decimal[intval(random_int(0,4))]['coin'] += $endcoin[0];
$decimal2 = ($endcoin[1] / 100);
$vali = 0;
while(true){
    $_val = $decimal[intval(random_int(0,4))]['coin'] + $decimal2;
    $coin = strval(number_format($decimal2, 2, '.', ''));
    $n1 = substr($coin,-2,1); //小数点第一位
    $n2 = substr($coin,-1);
    if((($n1+$n2) % 10)!=0 || $vali >= 4){
        break;
    }
    $vali ++;
}
$decimal[intval(random_int(0,4))]['coin'] += ($endcoin[1] / 100);
*/
# 生成红包
$redCoins = $this->redPacketRound($red['db'],10,0);

$total = 0;
foreach($redCoins as $k=>$v){
    echo $v['coin']."\n";
    $total += $v['coin'];
}
echo $total . "\n";

foreach($users as $k=>$v){
    //$redcoin = $this->randArrayCoin($redCoins);
    //$users[$k]['redcoin'] = $redcoin['coin'];
    //unset($redCoins[$redcoin['key']]);
    $redcoin = $redCoins[$k];
    $users[$k]['redcoin'] = $redcoin['coin'];

    $coin = strval(number_format($redcoin['coin'], 2, '.', ''));
    $n1 = substr($coin,-2,1); //小数点第一位
    $n2 = substr($coin,-1);   //小数点后二位

    $winsort_odds_key = 'n'.(($n1+$n2) % 10); // 牛几

    // 最后是最大的牌型：牛牛
    if((($n1+$n2) % 10)==0){
        $winsort_odds_key = "n0";
    }
    # ======================================================================================================
    # $redCoins[$k]['coin'] = $redcoin['coin'];
    //找出对应输赢的牌型顺序
    $users[$k]['winsort'] = $this->getOddsWinSort($game['odds'],'cardtype', $winsort_odds_key, 'winsort');
    //找出对应的赔率
    $users[$k]['odds'] = $this->getOddsWinSort($game['odds'],'cardtype', $winsort_odds_key, 'odds');
    //牌型名称
    $users[$k]['cardtitle'] = $this->getOddsWinSort($game['odds'],'cardtype', $winsort_odds_key, 'cardtitle');
    # 牌型赔率代码
    $users[$k]['cardtype'] = $winsort_odds_key;
    # 默认盈亏为0
    $users[$k]['gameprofit'] = 0;
    # 当前红包的用户类型
    switch ($k){
        case 0;
            $users[$k]['pointstype'] = 2;
            break;
        case 1:
            $users[$k]['pointstype'] = 1;
            break;
        case 2:
            $users[$k]['pointstype'] = 3;
            break;
        case 3:
            $users[$k]['pointstype'] = 1;
            break;
        case 4:
            $users[$k]['pointstype'] = 2;
            break;
    }

}

# 庄家的牌型
$sendUser = $users[0];
$player = $users;
# 去除庄家
unset($player[0]);

//庄家赢的总金额(以备收门票和抽水),闲家赢的每玩家分次收取
$senderUser_win_coin_total = 0;
$senderUser_los_coin_total = 0;

# 当前最大牌型的门票和抽水
$ticket = isset($game['odds'][0]['ticket']) ? $game['odds'][0]['ticket'] : 3;
$winerprofit = isset($game['odds'][0]['winerprofit']) ? $game['odds'][0]['winerprofit'] : 3;

# 计算门票、抽水及盈亏
foreach($users as $k=>$v)
{
    # 绕开庄家
    if($k > 0) {

        //默认是庄家赢
        $winer = array(
            'uid' => $sendUser['uid'],
            'cardtype' => $sendUser['cardtype'],
            'wincoin' => ($sendUser['redcoin'] * $sendUser['odds']), //默认按最大赔率支付
        );
        //庄家赢的标志
        $sender_win = true;
        //比牌型,winsort的值,如果winsort的值一样，即比进入的时间
        if ($v['winsort'] == $sendUser['winsort']) {
            # 如果两个人的牌型一样,即比拿到的金额大小
            if ($v['redcoin'] > $sendUser['redcoin']) {
                $winer['uid'] = $v['uid'];
                $sender_win = false;
            }
        } else {
            # 如果闲家的牌型比庄家的好
            if ($v['winsort'] > $sendUser['winsort']) {
                $winer['uid'] = $v['uid'];
                $sender_win = false;
            }
        }

        if ($sender_win) {
            # 庄家赢
            $sender_win_coin = $red['coin'] * $sendUser['odds'];
            $senderUser_win_coin_total += $sender_win_coin;
            # 交门票 - 抢到的红包金额 * 费率
            $ticketcoin = $this->formatCoin((($v['redcoin']) * $ticket) / 100);
            # 赢家抽水 - 抢到的红包金额 * 费率
            $winerprofitcoin = $this->formatCoin((($v['redcoin']) * $winerprofit) / 100);
            # 最终的盈亏
            $users[$k]['ticket'] = $ticketcoin;
            $users[$k]['winerprofit'] = $winerprofitcoin;
            # 最终的盈亏
            $users[$k]['gameprofit'] = $v['redcoin'] - ($sendUser['odds'] * $red['coin']) - $ticketcoin - $winerprofitcoin;

        } else {
            # 退回闲家的预投注金额
            # 按赔率计算赢到的金额
            $user_win_coin = $red['coin'] * $v['odds'];
            # 交门票 - (玩家赢到的金额 + 抢到的红包金额) * 费率
            $ticketcoin = $this->formatCoin((($user_win_coin + $v['redcoin']) * $ticket) / 100);
            # 赢家抽水 - (玩家赢到的金额 + 抢到的红包金额) * 费率
            $winerprofitcoin = $this->formatCoin((($user_win_coin + $v['redcoin']) * $winerprofit) / 100);
            # 最终的盈亏
            $users[$k]['ticket'] = $ticketcoin;
            $users[$k]['winerprofit'] = $winerprofitcoin;
            $users[$k]['gameprofit'] = ($red['coin'] * $v['odds']) + $v['redcoin'] - $ticketcoin - $winerprofitcoin;

            $senderUser_los_coin_total += $user_win_coin;
        }
    }
}
# 庄家的门票及抽水
//庄家赢利门票及抽水及把余下的预投注还给庄家
$sender_ticket = 0;
$sender_profit = 0;

$users[0]['winerprofit'] = $sender_profit;
$users[0]['ticket'] = $sender_ticket;

if($senderUser_win_coin_total > 0)
{
    # 计算门票及抽水的基数
    $tpcoin = $senderUser_win_coin_total + $sendUser['redcoin'];
    # 门票
    $sender_ticket = (floatval($tpcoin) *  floatval($ticket)) / 100;
    # 抽水
    $sender_profit = (floatval($tpcoin) *  floatval($winerprofit)) / 100;

    $users[0]['winerprofit'] = $sender_profit;
    $users[0]['ticket'] = $sender_ticket;
}
# 计算庄家的盈亏
$users[0]['gameprofit'] = $senderUser_win_coin_total - $senderUser_los_coin_total + $users[0]['redcoin'] - $sender_ticket - $sender_profit;

# ================ 一个最终的盈亏还要减去底包的金额

foreach ($users as $k=>$v){
    # 要计算玩家本次结算后 超出概率值的金额
    $users[$k]['overstep'] = 0;
    $pos = ($v['profit'] + $v['gameprofit']) / $v['recharge'] * 100;
    if($pos >= $v['chance']){
        $users[$k]['chancetype'] = 1;
        $users[$k]['overstep'] = ($pos - $v['chance']);

    }else{
        $users[$k]['chancetype'] = 0;
        $users[$k]['overstep'] = $pos;
    }
    $users[$k]['overstep'] = $this->formatCoin($pos);
}
?>
<div class="maintop">
    当前位置：游戏测试
    <a href="javascript:;" action="/index/desktop_list" id="btn_refresh" class="mainFrame button">刷新</a>
</div>
<div class="main-search">

</div>
<div id="mainbody">
    <table width="100%" class="tablist">
        <tr class="trbg">
            <th>牌号</th>
            <th>顺序</th>
            <th>牌型</th>
            <th>赔率</th>
            <th>牌值</th>
        </tr>
        <? foreach($game['odds'] as $k=>$v){ ?>
        <tr class="f9">
            <td><?=$v['cardtype'] ?></td>
            <td><?=$v['winsort'] ?></td>
            <td><?=$v['cardtitle'] ?></td>
            <td><?=$v['odds'] ?></td>
            <td style="text-align:left"><?=$v['cards'] ?></td>
        </tr>
        <? } ?>
    </table>
    <table width="100%" class="tablist">
        <tr class="trbg">
            <th>UID</th>
            <th>玩家类型</th>
            <th>昵称</th>
            <th>充值</th>
            <th>当前盈亏</th>
            <th>满概</th>
            <th>下注</th>
            <th>赔率</th>
            <th>红包</th>
            <th>牌号</th>
            <th>牌型</th>
            <th>门票</th>
            <th>抽水</th>
            <th>盈亏</th>
            <th>赢率</th>
            <th>前率</th>
            <th>后率</th>
        </tr>
        <?
        $c = array(
            'betcoin' => 0,
            'profit' => 0,
            'redcoin' => 0,
            'ticket' => 0,
            'winerprofit' => 0,
            'gameprofit' => 0,
        );
        foreach($users as $k=>$v){ ?>
        <tr class="f9">
            <td><?=$v['uid'] ?></td>
            <td><?
                switch ($v['pointstype']){
                    case 1:
                        echo '普通';
                        break;
                    case 2:
                        echo '概率';
                        break;
                    case 3:
                        echo '内部';
                        break;
                }
                ?></td>
            <td><?=$v['nickname'] ?></td>
            <td><?=$v['recharge'] ?></td>
            <td><?=$v['profit'] ?></td>
            <td class="<?=($v['chancetype'] ? 'green' : 'red')?>" style="text-align:center"><?=($v['chancetype'] ? '达标' : '未达')?></td>
            <td><?=$red['coin'] ?></td>
            <td><?=$v['odds'] ?></td>
            <td><?=$v['redcoin'] ?></td>
            <td><?=$v['cardtype'] ?></td>
            <td><?=$v['cardtitle'] ?></td>
            <td><?=$v['ticket'] ?></td>
            <td><?=$v['winerprofit'] ?></td>
            <td><?=$v['gameprofit'] ?></td>
            <td><?=$v['chance'] ?>%</td>
            <td><?=$this->formatCoin($v['profit'] / $v['recharge'] * 100) ?>%</td>
            <td><?=$v['overstep'] ?>%</td>
        </tr>
        <?
            $c['betcoin'] += $v['betcoin'];
            $c['profit'] += $v['betcoin'];
            $c['redcoin'] += $v['redcoin'];
            $c['ticket'] += $v['ticket'];
            $c['winerprofit'] += $v['winerprofit'];
            $c['gameprofit'] += $v['gameprofit'];
        } ?>
        <tr class="f9">
            <td colspan="4">合计</td>
            <td><?=$c['betcoin'] ?></td>
            <td></td>
            <td><?=$c['profit'] ?></td>
            <td></td>
            <td><?=$c['redcoin'] ?></td>

            <td></td>
            <td><?=($c['ticket'] + $c['winerprofit']) ?></td>
            <td><?=$c['ticket'] ?></td>
            <td><?=$c['winerprofit'] ?></td>
            <td><?=($c['gameprofit'] - $red['db']) ?></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
    </table>
        <?php
        # 如果庄家已达标（要分庄家是 概率玩家 还是 普通玩家 还是 内部玩家）
        # 按后概率从小到大排序
        $users = $this->sortArray($users, "overstep", "ASC");
        # 重新变换红包 - 把最大的红包换给最小后概率的用户
        $redCoins = $this->sortArray($users, "winsort", "ASC");
        # 按后概率从小到大把红包从大到小进行分配
        for($i=0;$i<=4;$i++){
            $users[$i]['redcoin'] = $redCoins[(4-$i)]['redcoin'];
            $users[$i]['winsort'] = $redCoins[(4-$i)]['winsort'];
            $users[$i]['ticket'] = $redCoins[(4-$i)]['ticket'];
            $users[$i]['winerprofit'] = $redCoins[(4-$i)]['winerprofit'];
            $users[$i]['cardtitle'] = $redCoins[(4-$i)]['cardtitle'];
            $users[$i]['cardtype'] = $redCoins[(4-$i)]['cardtype'];
        }
        ?>
            <table width="100%" class="tablist">
                <tr class="trbg">
                    <th>UID</th>
                    <th>玩家类型</th>
                    <th>昵称</th>
                    <th>充值</th>
                    <th>当前盈亏</th>
                    <th>满概</th>
                    <th>下注</th>
                    <th>赔率</th>
                    <th>红包</th>
                    <th>牌号</th>
                    <th>牌型</th>
                    <th>门票</th>
                    <th>抽水</th>
                    <th>盈亏</th>
                    <th>赢率</th>
                    <th>前率</th>
                    <th>后率</th>
                </tr>
                <?
                $c = array(
                    'betcoin' => 0,
                    'profit' => 0,
                    'redcoin' => 0,
                    'ticket' => 0,
                    'winerprofit' => 0,
                    'gameprofit' => 0,
                );
                foreach($users as $k=>$v){ ?>
                    <tr class="f9">
                        <td><?=$v['uid'] ?></td>
                        <td><?
                            switch ($v['pointstype']){
                                case 1:
                                    echo '普通';
                                    break;
                                case 2:
                                    echo '概率';
                                    break;
                                case 3:
                                    echo '内部';
                                    break;
                            }
                            ?></td>
                        <td><?=$v['nickname'] ?></td>
                        <td><?=$v['recharge'] ?></td>
                        <td><?=$v['profit'] ?></td>
                        <td class="<?=($v['chancetype'] ? 'green' : 'red')?>" style="text-align:center"><?=($v['chancetype'] ? '达标' : '未达')?></td>
                        <td><?=$red['coin'] ?></td>
                        <td><?=$v['odds'] ?></td>
                        <td><?=$v['redcoin'] ?></td>
                        <td><?=$v['cardtype'] ?></td>
                        <td><?=$v['cardtitle'] ?></td>
                        <td><?=$v['ticket'] ?></td>
                        <td><?=$v['winerprofit'] ?></td>
                        <td><?=$v['gameprofit'] ?></td>
                        <td><?=$v['chance'] ?>%</td>
                        <td><?=$this->formatCoin($v['profit'] / $v['recharge'] * 100) ?>%</td>
                        <td><?=$v['overstep'] ?>%</td>
                    </tr>
                    <?
                    $c['betcoin'] += $v['betcoin'];
                    $c['profit'] += $v['betcoin'];
                    $c['redcoin'] += $v['redcoin'];
                    $c['ticket'] += $v['ticket'];
                    $c['winerprofit'] += $v['winerprofit'];
                    $c['gameprofit'] += $v['gameprofit'];
                } ?>
                <tr class="f9">
                    <td colspan="4">合计</td>
                    <td><?=$c['betcoin'] ?></td>
                    <td></td>
                    <td><?=$c['profit'] ?></td>
                    <td></td>
                    <td><?=$c['redcoin'] ?></td>

                    <td></td>
                    <td><?=($c['ticket'] + $c['winerprofit']) ?></td>
                    <td><?=$c['ticket'] ?></td>
                    <td><?=$c['winerprofit'] ?></td>
                    <td><?=($c['gameprofit'] - $red['db']) ?></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
            </table>
</div>
</div>
